package com.hunt.util;

import org.omg.CORBA.PUBLIC_MEMBER;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

/**
 * @Author sjs
 * @Date 2020/10/21
 * @Description 极光推送工具类
 */
public class AuroraUtil {

    public static String APP_KEY = "aa8909482e5fdd13491c53bc";
    public static String MASTER_KEY = "37fd85c8fb101f9cbe466acb";

    public static String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJW6F1CK16A7nEQd" +
            "UcxMEkXT9CBtcGAd5gpqP1Sr3dNrRl4PP9wKAr8KZ8r61kymmPEqj+fLU0pmJcUd" +
            "y9DiQpKPTFGGSxKvHpWFqj3kQXVWl6Co9Drtw5hywBvFVAkSIH0X2gyMPftI2KHc" +
            "FcEAGmhKH/QXoECELlLS1Leb6ldlAgMBAAECgYEAhvmjWvBPQht628yymMFbF6fj" +
            "Q/hkfcF2R51QSjxFVPo4DUMiBnLf1ujCeLF3ztHAvmfoIsWg6Ej9c51Qx5Ww/J1D" +
            "nslht+HOwMfTD7AL0dzQTDKzCoaJFAS63A5/IYG3vh7toOBdtukKJZrGJk/Eg49c" +
            "AOoyV3spjvMa05bj02ECQQDEz6XX+IM/WO5lMgYiJHYI+bv5RkPYsmrxlbJDWyvY" +
            "kAP0zfex8jluMdAoOjARIzN30K2Ulqiw83jrzpXxFUjfAkEAwsF2e76JLoEKNIYe" +
            "2vzJDMi8FKld5DVtFuNYhBuHbPeIU2xP3Gqlkq7MRgXsAc+jqbgQ7YOJalRa+WN9" +
            "WTL0OwJAURjzUJwRkP/XNBG530XA8Y/eeigO+vMCCExT6VdfKNynFK/uef/C1Hpo" +
            "OLtJZTZpFd0CObEfD9Mzp483z+CiZwJATN8n8TgH1b9BU/IMg8t7X/xroIwpb0ZX" +
            "RM1/xsVG8w/RU3xRGWNO71LndkwoF57prrfklptwxfN95SoL9AVUuQJBAI0WLEyz" +
            "Eh78Bdz5YLVOF6JkS5BkrIF0cm1Yqokq31QbnTEyoqLd9OZX5+vIvMtf2Hvah5YC" +
            "t2Mm/UW9guAy3B0=";

    public static String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVuhdQitegO5xEHVHMTBJF0/Qg" +
            "bXBgHeYKaj9Uq93Ta0ZeDz/cCgK/CmfK+tZMppjxKo/ny1NKZiXFHcvQ4kKSj0xR" +
            "hksSrx6Vhao95EF1VpegqPQ67cOYcsAbxVQJEiB9F9oMjD37SNih3BXBABpoSh/0" +
            "F6BAhC5S0tS3m+pXZQIDAQAB";



//    public static String decrypt(String cryptograph, String priKey) throws Exception {
//        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(priKey));
//        PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
//
//        Cipher cipher=Cipher.getInstance("RSA");
//        cipher.init(Cipher.DECRYPT_MODE, privateKey);
//
//        byte [] b = Base64.getDecoder().decode(cryptograph);
//
//        return new String(cipher.doFinal(b));
//    }
    // 使用私钥解密数据
    public static String decrypt(String encryptedData,String privateKey) throws Exception {
        PrivateKey pvk=loadPrivateKey(privateKey);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, pvk);
        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedData);
    }
    public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }
}
